package joo.english.test;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class Word extends Activity implements AdapterView.OnItemSelectedListener, View.OnClickListener, CompoundButton.OnCheckedChangeListener{	
	
	private Spinner levelSel;	
	private ListView wordList;
	private ArrayList<String> listLevel;	
	private DTO dto;
	private DAO dao;
	private ArrayList<DTO> list;
	private CustomAdapter customAdapter;
	private TextView indexAll, indexA, indexB, indexC, indexD, indexE, indexF, indexG, indexH, indexI,
	 indexJ, indexK, indexL, indexM, indexN, indexO, indexP, indexQ, indexR, indexS,
	 indexT, indexU, indexV, indexW, indexX, indexY, indexZ;
	
	final private String[] LEVEL = {"모두보기", "즐겨찾기", "맞춘단어", "틀린단어"};
	
    @Override
	protected void onDestroy() {		
		super.onDestroy();		
	}

	public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.word);        
        
        
        indexAll = (TextView)findViewById(R.id.indexAll); indexAll.setOnClickListener(this);
        indexA = (TextView)findViewById(R.id.indexA); indexA.setOnClickListener(this); 
        indexB = (TextView)findViewById(R.id.indexB); indexB.setOnClickListener(this);
        indexC = (TextView)findViewById(R.id.indexC); indexC.setOnClickListener(this);
        indexD = (TextView)findViewById(R.id.indexD); indexD.setOnClickListener(this);
        indexE = (TextView)findViewById(R.id.indexE); indexE.setOnClickListener(this);
        indexF = (TextView)findViewById(R.id.indexF); indexF.setOnClickListener(this);
        indexG = (TextView)findViewById(R.id.indexG); indexG.setOnClickListener(this);
        indexH = (TextView)findViewById(R.id.indexH); indexH.setOnClickListener(this);
        indexI = (TextView)findViewById(R.id.indexI); indexI.setOnClickListener(this);
        indexJ = (TextView)findViewById(R.id.indexJ); indexJ.setOnClickListener(this);
        indexK = (TextView)findViewById(R.id.indexK); indexK.setOnClickListener(this);
        indexL = (TextView)findViewById(R.id.indexL); indexL.setOnClickListener(this);
        indexM = (TextView)findViewById(R.id.indexM); indexM.setOnClickListener(this);
        indexN = (TextView)findViewById(R.id.indexN); indexN.setOnClickListener(this);
        indexO = (TextView)findViewById(R.id.indexO); indexO.setOnClickListener(this);
        indexP = (TextView)findViewById(R.id.indexP); indexP.setOnClickListener(this);
        indexQ = (TextView)findViewById(R.id.indexQ); indexQ.setOnClickListener(this);
        indexR = (TextView)findViewById(R.id.indexR); indexR.setOnClickListener(this);
        indexS = (TextView)findViewById(R.id.indexS); indexS.setOnClickListener(this);
        indexT = (TextView)findViewById(R.id.indexT); indexT.setOnClickListener(this);
        indexU = (TextView)findViewById(R.id.indexU); indexU.setOnClickListener(this);
        indexV = (TextView)findViewById(R.id.indexV); indexV.setOnClickListener(this);
        indexW = (TextView)findViewById(R.id.indexW); indexW.setOnClickListener(this);
        indexX = (TextView)findViewById(R.id.indexX); indexX.setOnClickListener(this);
        indexY = (TextView)findViewById(R.id.indexY); indexY.setOnClickListener(this);
        indexZ = (TextView)findViewById(R.id.indexZ); indexZ.setOnClickListener(this);
        
        
                
        // Spinner로 메뉴 선택
        listLevel = new ArrayList<String>();
        listLevel.add(LEVEL[0]);
        listLevel.add(LEVEL[1]);
        listLevel.add(LEVEL[2]);
        listLevel.add(LEVEL[3]);        
        
        levelSel = (Spinner)findViewById(R.id.levelSel);
        wordList = (ListView)findViewById(R.id.wordList);
        
        ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listLevel);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
        levelSel.setAdapter(aa);        
        levelSel.setOnItemSelectedListener(this);
    }
	
	// Spinner 로 메뉴선택
	
	public void onItemSelected(AdapterView parent, View v, int position, long id){
		Adapter a = parent.getAdapter();
		String item = (String)a.getItem(position);
		if(item.equals(LEVEL[0])){
			setList(0, "All");
			allClear();
			indexAll.setBackgroundColor(Color.GREEN);
		}else if(item.equals(LEVEL[1])){
			setList(1, "All");
			allClear();
			indexAll.setBackgroundColor(Color.GREEN);
		}else if(item.equals(LEVEL[2])){
			setList(2, "All");
			allClear();
			indexAll.setBackgroundColor(Color.GREEN);
		}else if(item.equals(LEVEL[3])){
			setList(3, "All");
			allClear();
			indexAll.setBackgroundColor(Color.GREEN);
		}else setList(0, "A"); allClear(); indexAll.setBackgroundColor(Color.GREEN);
	}
	public void onNothingSelected(AdapterView parent){
	}
	
	// 알파벳 클릭시 색인
		public void onClick(View v) {
			TextView tempTextView = (TextView)findViewById(v.getId());
			
			int level = levelSel.getSelectedItemPosition();
			Log.i("Selected Level",""+level);
			setList(level, tempTextView.getText().toString());			
			allClear();
			v.setBackgroundColor(Color.GREEN);
			
			
			
		}
		
	private void setList(int level, String index){
		dao = new DAO(this);		
		list = dao.getList(level, index);
		
		customAdapter = new CustomAdapter(this, R.layout.word_item, list, dao);
		Log.i("customAdapter.getCount()", ""+customAdapter.getCount());
		wordList.setAdapter(customAdapter);
	}
	
	private void allClear(){
		indexAll.setBackgroundColor(Color.TRANSPARENT);
		indexA.setBackgroundColor(Color.TRANSPARENT);
		indexB.setBackgroundColor(Color.TRANSPARENT);
		indexC.setBackgroundColor(Color.TRANSPARENT);
		indexD.setBackgroundColor(Color.TRANSPARENT);
		indexE.setBackgroundColor(Color.TRANSPARENT);
		indexF.setBackgroundColor(Color.TRANSPARENT);
		indexG.setBackgroundColor(Color.TRANSPARENT);
		indexH.setBackgroundColor(Color.TRANSPARENT);
		indexI.setBackgroundColor(Color.TRANSPARENT);
		indexJ.setBackgroundColor(Color.TRANSPARENT);
		indexK.setBackgroundColor(Color.TRANSPARENT);
		indexL.setBackgroundColor(Color.TRANSPARENT);
		indexM.setBackgroundColor(Color.TRANSPARENT);
		indexN.setBackgroundColor(Color.TRANSPARENT);
		indexO.setBackgroundColor(Color.TRANSPARENT);
		indexP.setBackgroundColor(Color.TRANSPARENT);
		indexQ.setBackgroundColor(Color.TRANSPARENT);
		indexR.setBackgroundColor(Color.TRANSPARENT);
		indexS.setBackgroundColor(Color.TRANSPARENT);
		indexT.setBackgroundColor(Color.TRANSPARENT);
		indexU.setBackgroundColor(Color.TRANSPARENT);
		indexV.setBackgroundColor(Color.TRANSPARENT);
		indexW.setBackgroundColor(Color.TRANSPARENT);
		indexX.setBackgroundColor(Color.TRANSPARENT);
		indexY.setBackgroundColor(Color.TRANSPARENT);
		indexZ.setBackgroundColor(Color.TRANSPARENT);
	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		
	}
	
		
}

class CustomAdapter extends android.widget.BaseAdapter{
	
	Context context;
	int layoutId;
	ArrayList<DTO> list;
	LayoutInflater inflater;
	TextView wordName, wordMean, wordLearn,wordBookmark;
	DAO dao;
	
	
	
	CustomAdapter(Context context, int layoutId, ArrayList<DTO> list, DAO dao){
		this.context = context;
		this.layoutId = layoutId;
		this.list = list;
		this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.dao = dao;
	}

	public int getCount() {		
		return list.size();
	}

	public Object getItem(int position) {		
		return list.get(position);
	}

	public long getItemId(int position) {		
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		if(convertView == null){
			convertView = inflater.inflate(layoutId, parent, false);
		}
		
		wordName = (TextView)convertView.findViewById(R.id.wordName);
		wordMean = (TextView)convertView.findViewById(R.id.wordMean);
		wordLearn = (TextView)convertView.findViewById(R.id.wordLearn);
		
		wordBookmark = (TextView)convertView.findViewById(R.id.wordBookmark);
		
		
		if(list.get(position).getBookmark()==1){			
			wordBookmark.setText("★");			
			wordBookmark.setTag(list.get(position));			
		}else{
			wordBookmark.setText("☆");			
			wordBookmark.setTag(list.get(position));			
		}
		wordBookmark.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				TextView temp = (TextView)v;
				DTO dto = (DTO)v.getTag();
				if(dto.getBookmark() == 1){
					dto.setBookmark(0);
					v.setTag(dto);
					dao.changeBookmark(dto.getWord(), 0);
					temp.setText("☆");					
					Log.i("click", "false 로 수정");
				}else if(dto.getBookmark() == 0){
					dto.setBookmark(1);
					v.setTag(dto);
					dao.changeBookmark(dto.getWord(), 1);
					temp.setText("★");			
					Log.i("click", "true 로 수정");
				}
			}
		});
		
		
		/*wordBookmark.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				String targetWord = buttonView.getTag().toString();
				dao.changeBookmark(targetWord, isChecked);
				Log.i("변화","바꿀 단어Tag : "+buttonView.getTag().toString()+"를 "+isChecked);
				
			}
		});*/	
		
		wordName.setText(list.get(position).getWord());
		wordMean.setText(list.get(position).getMean());		
		//Log.i("bookMark", list.get(position).getWord()+" = "+list.get(position).getBookmark());
		
		switch(list.get(position).getLearn()){
			case 0: wordLearn.setText(""); break;
			case 1: wordLearn.setText("O"); break; // 맞춘 단어
			case 2: wordLearn.setText("X"); break; // 틀린 단어
		}
		return convertView;
	}		
}